package com.cykj.pos.controller.api;

import com.cykj.common.annotation.Log;
import com.cykj.common.core.controller.BaseController;
import com.cykj.common.core.domain.AjaxResult;
import com.cykj.common.core.page.TableDataInfo;
import com.cykj.common.enums.BusinessType;
import com.cykj.common.utils.poi.ExcelUtil;
import com.cykj.pos.domain.BizDayStatistics;
import com.cykj.pos.service.IBizDayStatisticsService;
import com.cykj.pos.service.IBizMicroInfoService;
import com.cykj.pos.util.LoginUserUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * statisticsController
 *
 * @author weijianbo
 * @date 2024-05-05
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/pos/statistics" )
public class BizDayStatisticsController extends BaseController {

    private final IBizDayStatisticsService iBizDayStatisticsService;
    @Autowired
    private IBizMicroInfoService bizMicroInfoService;
    /**
     * 查询statistics列表
     */
    @PreAuthorize("@ss.hasPermi('pos:statistics:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizDayStatistics bizDayStatistics) {
        String userId = LoginUserUtils.getLoginUserId().toString();
        String dftype =  bizMicroInfoService.getDfTypeByMerchId(userId);
        if(dftype != null && !"".equals(dftype) && !"1".equals(userId)){
            bizDayStatistics.setVar2(dftype);
        }
        startPage();
        List<BizDayStatistics> list = iBizDayStatisticsService.queryList(bizDayStatistics);
        return getDataTable(list);
    }


    /**
     * 导出statistics列表
     */
    @PreAuthorize("@ss.hasPermi('pos:statistics:export')" )
    @Log(title = "statistics" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(BizDayStatistics bizDayStatistics) {
        String userId = LoginUserUtils.getLoginUserId().toString();
        String dftype =  bizMicroInfoService.getDfTypeByMerchId(userId);
        if(dftype != null && !"".equals(dftype) && !"1".equals(userId)){
            bizDayStatistics.setVar2(dftype);
        }
        List<BizDayStatistics> list = iBizDayStatisticsService.queryList(bizDayStatistics);
        ExcelUtil<BizDayStatistics> util = new ExcelUtil<BizDayStatistics>(BizDayStatistics.class);
        return util.exportExcel(list, "statistics" );
    }

    /**
     * 获取statistics详细信息
     */
    @PreAuthorize("@ss.hasPermi('pos:statistics:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iBizDayStatisticsService.getById(id));
    }

    /**
     * 新增statistics
     */
    @PreAuthorize("@ss.hasPermi('pos:statistics:add')" )
    @Log(title = "statistics" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizDayStatistics bizDayStatistics) {
        return toAjax(iBizDayStatisticsService.save(bizDayStatistics) ? 1 : 0);
    }

    /**
     * 修改statistics
     */
    @PreAuthorize("@ss.hasPermi('pos:statistics:edit')" )
    @Log(title = "statistics" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizDayStatistics bizDayStatistics) {
        return toAjax(iBizDayStatisticsService.updateById(bizDayStatistics) ? 1 : 0);
    }

    /**
     * 删除statistics
     */
    @PreAuthorize("@ss.hasPermi('pos:statistics:remove')" )
    @Log(title = "statistics" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iBizDayStatisticsService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
